import 'dart:io';

import 'package:changxindianzishangwu/cache/local_cache.dart';
import 'package:changxindianzishangwu/dao/upload_file_dao.dart';
import 'package:changxindianzishangwu/else/lib/common/index.dart';
import 'package:changxindianzishangwu/http/base/http_urls.dart';
import 'package:changxindianzishangwu/util/app_color.dart';
import 'package:changxindianzishangwu/util/colors.dart';
import 'package:changxindianzishangwu/util/toast.dart';
import 'package:changxindianzishangwu/util/universal_view.dart';
import 'package:changxindianzishangwu/util/update/common.dart';
import 'package:changxindianzishangwu/util/update/update_info_model.dart';
import 'package:changxindianzishangwu/util/update/update_prompter.dart';
import 'package:changxindianzishangwu/widget/app_bar.dart';
import 'package:changxindianzishangwu/widget/web_view.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:package_info/package_info.dart';

class AboutUsPage extends StatefulWidget {
  AboutUsPage({Key key}) : super(key: key);

  @override
  _AboutUsPageState createState() => _AboutUsPageState();
}

class _AboutUsPageState extends State<AboutUsPage> {
  String versionCode;

  @override
  void initState() {
    getVerName();
    // TODO: implement initState
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: TopAppBar(context, "关于我们"),
      body: ListView(
        children: [
          Container(
            width: double.infinity,
            color: Colors.white,
            padding: EdgeInsets.only(top: height(50), bottom: height(45)),
            child: Center(
              child: Column(
                children: [
                  UniversalView().ImageClip(
                      path: "images/ic_launcher.png", width: 80, height: 80),
                  Text(
                    "版本号：$versionCode",
                    style: TextStyle(fontSize: ScreenUtil().setSp(15)),
                  ),
                  Text(
                    HttpUrls.isOnline ? "(公测服)" : "(内测服)",
                    style: TextStyle(fontSize: ScreenUtil().setSp(15)),
                  ),
                ],
              ),
            ),
          ),

          _item("用户服务协议", () {
            Get.to(WebViewExample(
              url: HttpUrls.YH_XY,
            ));
          }),
          UniversalView().line(),
          _item("隐私政策", () {
            Get.to(WebViewExample(
              url: HttpUrls.YS_XY,
            ));
          }),
          UniversalView().line(),
          _item("动态发布条款", () {
            Get.to(WebViewExample(
              url: HttpUrls.DT,
            ));
          }),
          UniversalView().line(),
          _item("检查更新", () {
            checkUpdate();
          }),
          // UniversalView().line(),
          // _item("去评分",(){
          //
          // }),
          // UniversalView().line(),
        ],
      ),
    );
  }

  getVerName() async {
    await CommonUtils.getVersionName().then((value) {
      print('------版本号：$value');
      setState(() {
        versionCode = value;
      });
    });
  }

  InkWell _item(title, function) {
    return InkWell(
      key: UniqueKey(),
      onTap: function,
      child: Container(
        height: 60,
        color: Colors.white,
        child: ListTile(
          leading: Text(
            title,
            style: TextStyle(fontSize: ScreenUtil().setSp(15)),
          ),
          trailing: Icon(Icons.keyboard_arrow_right),
        ),
      ),
    );
  }

  void checkUpdate() async {
    EasyLoading.show();
    UpdateInfoModel _updateInfo = await UpLoadFileDao.checkVersion();
    EasyLoading.dismiss();
    if (_updateInfo.resultCode == 1) {
      String versionCode = await CommonUtils.getVersionCode();
      _updateInfo.data.isForce = false;

      int _zxVersionCODE = int.parse(versionCode);

      ///线上更新版本
      int _onlineCode = (Platform.isAndroid
          ? _updateInfo.data.androidVersion
          : _updateInfo.data.iosVersion);

      ///本地版本
      String _qzVersion = Platform.isAndroid
          ? _updateInfo.data.androidDisable
          : _updateInfo.data.iosDisable;

      ///强制更新版本
      print('--------强制更新 _zxVersionCODE$_zxVersionCODE');
      print('--------强制更新 _onlineCode$_onlineCode');
      print('--------强制更新 _qzVersion$_qzVersion');
      // _zxVersionCODE = 129;
      if (_zxVersionCODE < _onlineCode) {
        ///androidDisable 不等于空且 大于本地版本号 强制更新
        if (!ObjectUtil.isEmptyString(_qzVersion) &&
            int.parse(versionCode) < int.parse(_qzVersion)) {
          _updateInfo.data.isForce = true;
          UpdatePrompter(
              updateEntity: _updateInfo.data,
              onInstall: (String filePath) {
                CommonUtils.installAPP(filePath);
              }).show(context);
        }

        UpdatePrompter(
            updateEntity: _updateInfo.data,
            onInstall: (String filePath) {
              CommonUtils.installAPP(filePath);
            }).show(context);
      } else
        toast("已是最新版本");
    }
  }
}
